#include"queue.h"
//init
void Qinit(queue* pq)
{
	assert(pq);
	pq->phead = pq->ptail = NULL;
}
//pushtail
void Qpush(queue* pq, DataType x)
{
	assert(pq);
	node* newnode = (node*)malloc(sizeof(node));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->phead == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
}
//is empty?
bool Qisempty(queue* pq)
{
	assert(pq);
	return pq->phead == NULL;
}
//pop
void Qpop(queue* pq)
{
	assert(!Qisempty(pq));
	if (pq->phead == pq->ptail)
	{
		free(pq->phead);
		pq->phead=pq->ptail = NULL;
	}
	else
	{
		node* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
}
//destroy
void Qdestroy(queue* pq)
{
	assert(!Qisempty(pq));
	node* pcur = pq->phead;
	while (pcur)
	{
		pcur = pcur->next;
		free(pq->phead);
		pq->phead = pcur;
	}
	pq->phead = pq->ptail = NULL;
}
//front
DataType QFront(queue* pq)
{
	assert(!Qisempty(pq));
	return pq->phead->data;
}